package com.walixiwa.player.widget;

import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.Animation;
import android.widget.FrameLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.dueeeke.videoplayer.controller.ControlWrapper;
import com.dueeeke.videoplayer.controller.IControlComponent;
import com.dueeeke.videoplayer.player.VideoView;
import com.walixiwa.videoviewcontral.R;

/**
 * 播放器顶部标题栏
 */
public class DetailVodSpeedView extends FrameLayout implements IControlComponent {

    private ControlWrapper mControlWrapper;

    private TextView mTv050;
    private TextView mTv075;
    private TextView mTv100;
    private TextView mTv125;
    private TextView mTv150;
    private TextView mTv200;

    public DetailVodSpeedView(@NonNull Context context) {
        super(context);
    }

    public DetailVodSpeedView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public DetailVodSpeedView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    {
        this.setVisibility(GONE);
        LayoutInflater.from(getContext()).inflate(R.layout.detail_layout_speed_view, this, true);
        mTv050 = findViewById(R.id.tv_050);
        mTv075 = findViewById(R.id.tv_075);
        mTv100 = findViewById(R.id.tv_100);
        mTv125 = findViewById(R.id.tv_125);
        mTv150 = findViewById(R.id.tv_150);
        mTv200 = findViewById(R.id.tv_200);
        mTv050.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onSpeedClickListener != null) {
                    setChecked(0);
                    mControlWrapper.setSpeed(0.5f);
                    onSpeedClickListener.onSpeedClick("x0.50", 0.5f);
                }
            }
        });
        mTv075.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onSpeedClickListener != null) {
                    setChecked(1);
                    mControlWrapper.setSpeed(0.75f);
                    onSpeedClickListener.onSpeedClick("x0.75", 0.75f);
                }
            }
        });
        mTv100.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onSpeedClickListener != null) {
                    setChecked(2);
                    mControlWrapper.setSpeed(1.0f);
                    onSpeedClickListener.onSpeedClick("x1.00", 1.0f);
                }
            }
        });
        mTv125.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onSpeedClickListener != null) {
                    setChecked(3);
                    mControlWrapper.setSpeed(1.25f);
                    onSpeedClickListener.onSpeedClick("x1.25", 1.25f);
                }
            }
        });
        mTv150.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onSpeedClickListener != null) {
                    setChecked(4);
                    mControlWrapper.setSpeed(1.5f);
                    onSpeedClickListener.onSpeedClick("x1.50", 1.5f);
                }
            }
        });
        mTv200.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onSpeedClickListener != null) {
                    setChecked(5);
                    mControlWrapper.setSpeed(2.0f);
                    onSpeedClickListener.onSpeedClick("x2.00", 2.0f);
                }
            }
        });
    }

    @Override
    public void attach(@NonNull ControlWrapper controlWrapper) {
        mControlWrapper = controlWrapper;
    }

    @Override
    public View getView() {
        return this;
    }

    @Override
    public void onVisibilityChanged(boolean isVisible, Animation anim) {
        if (isVisible) {
            setVisibility(GONE);
        }
    }

    @Override
    public void onPlayStateChanged(int playState) {
        setVisibility(GONE);
    }

    @Override
    public void onPlayerStateChanged(int playerState) {
        setVisibility(GONE);
    }

    @Override
    public void setProgress(int duration, int position) {

    }

    @Override
    public void onLockStateChanged(boolean isLocked) {

    }

    private OnSpeedClickListener onSpeedClickListener;

    public void setOnSpeedClickListener(OnSpeedClickListener onSpeedClickListener) {
        this.onSpeedClickListener = onSpeedClickListener;
    }

    public interface OnSpeedClickListener {
        void onSpeedClick(String name, float speed);
    }

    private void setChecked(int index) {
        int checkedColor = getResources().getColor(R.color.colorPrimary);
        int defaultColor = Color.WHITE;
        mTv050.setTextColor(index == 0 ? checkedColor : defaultColor);
        mTv075.setTextColor(index == 1 ? checkedColor : defaultColor);
        mTv100.setTextColor(index == 2 ? checkedColor : defaultColor);
        mTv125.setTextColor(index == 3 ? checkedColor : defaultColor);
        mTv150.setTextColor(index == 4 ? checkedColor : defaultColor);
        mTv200.setTextColor(index == 5 ? checkedColor : defaultColor);
        setVisibility(GONE);
    }

    public void reset() {
        mControlWrapper.setSpeed(1.0f);
        setChecked(2);
    }

    public void show() {
        mControlWrapper.hide();
        this.setVisibility(VISIBLE);
        bringToFront();
    }
}
